<template>
  <div class="detail-container">
    <el-form ref="ruleForm" :model="userInfo" :rules="rules" label-width="100px">
      <el-form-item label="姓名：" prop="username">
        <el-input v-model="userInfo.username" placeholder="管理员" />
      </el-form-item>
      <el-form-item label="工号：" prop="workNumber">
        <el-input v-model="userInfo.workNumber" placeholder="1000" :disabled="isEdit" />
      </el-form-item>
      <el-form-item label="手机：" prop="mobile">
        <el-input v-model="userInfo.mobile" placeholder="13800000002" :disabled="isEdit" />
      </el-form-item>
      <el-form-item label="部门：" prop="departmentId">
        <EmployeeDepartment v-model="userInfo.departmentId" />
      </el-form-item>
      <el-form-item label="聘用形式：" prop="formOfEmployment">
        <el-select v-model="userInfo.formOfEmployment" placeholder="正式" clearable>
          <el-option
            label="正式"
            :value="1"
          />
          <el-option
            label="非正式"
            :value="2"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间：" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          format="yyyy年MM月dd日"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="转正时间：" prop="correctionTime">
        <el-date-picker
          v-model="userInfo.correctionTime"
          type="date"
          placeholder="选择日期"
          format="yyyy年MM月dd日"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="员工头像：" prop="staffPhoto" />
      <UploadImage :value.sync="userInfo.staffPhoto" />
      <el-form-item>
        <el-button type="primary" @click="submitForm">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import EmployeeDepartment from '@/views/employee/components/employeeDepartment.vue'
import { addEmployeeRequest, getEmployeeInfoRequest, updateEmployeeInfoRequest } from '@/api/employee'
import UploadImage from '@/views/employee/components/upload-image.vue'
export default {
  name: 'EmployeeDetail',
  components: {
    EmployeeDepartment,
    UploadImage
  },
  data() {
    const that = this
    const validateCorrectionTime = function(rule, value, callback) {
      console.log(that.userInfo.timeOfEntry, value)
      // 时间戳获取： +new Date()  Date.now()  new Date().getTime()
      if (+new Date(that.userInfo.timeOfEntry) > +new Date(value)) {
        callback(new Error('转正时间不能早于入职时间'))
        // return
      } else {
        callback()
      }
    }
    // 业务校验
    return {
      userInfo: {
        username: '',
        workNumber: '',
        mobile: '',
        departmentId: 1,
        formOfEmployment: 1,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: ''
      },

      rules: {
        // 姓名
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名位数在1-4位', trigger: 'blur' }
        ],
        // 手机号
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3|5|6|7|8|9]\d{9}$/, message: '手机号格式不对', trigger: 'blur' }
        ],
        // 部门
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        // 聘用形式
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'change' }
        ],
        // 入职时间
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'change' }
        ],
        // 转正时间
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'change' },
          { 'validator': validateCorrectionTime, 'trigger': 'change' }
        ]
      },

      value: []

    }
  },
  computed: {
    isEdit() {
      return !!this.$route.params.id
    }
  },
  created() {
    if (this.$route.params.id) {
      this.getEmployeeInfo()
    }
  },
  methods: {
    // 获取员工信息
    async getEmployeeInfo() {
      const res = await getEmployeeInfoRequest(this.$route.params.id)
      this.userInfo = res.data
    },
    // 提交 表单
    submitForm() {
      this.$refs.ruleForm.validate(async vali => {
        if (vali) {
          try {
            if (this.isEdit) {
              await updateEmployeeInfoRequest(this.userInfo)
            } else {
              await addEmployeeRequest(this.userInfo)
            }
            this.$message.success('添加员工成功')
          } catch (error) {
            this.$message.error('添加员工失败')
          }
          this.$router.back()
        }
      })
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<style lang="scss" scoped>
.detail-container{
  margin: 100px;
  .el-input{
    width: 400px;
  }
  ::v-deep .el-input{
    width: 400px;
    font-size: 20px;
    .el-input__inner{
      height: 28px;
    }
  }

}
</style>
